use dataset, clear

gen inft = .
replace inft = ft_dem if pid3 == "Democrat"
replace inft = ft_rep if pid3 == "Republican"

gen Br = .
gen Bd = .

replace ideology = -abs(ideology-4)

*** panels (a) and (b) inft by folded idoelogy ***

preserve

	tab ideology
	keep if inlist(ideology, -3, 0)
	
	collapse inft [aw=weight], by(ideology year pid3) 

	keep if !mi(ideology)

	tabulate ideology, gen(ideology_)
	
	
	#delimit;

	gr tw
		(sc inft year if ideology_1 == 1 & pid3 == "Democrat", col(gray))
		(fpfit inft year if ideology_1 == 1 & pid3 == "Democrat", col(black))
		(sc inft year if ideology_2 == 1 & pid3 == "Democrat", col(gray))
		(fpfit inft year if ideology_2 == 1 & pid3 == "Democrat", col(black))	
		,
			legend(
				order(1 "Ideologues" 3 "Moderates" 4 "Polyonmial")
				rows(1)
			)
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Year")		
			title("Democrats")	
			ytitle("Average own-party rating")
			name(a, replace)
			xlab(1972(8)2016)
			;

	#delimit cr

	gr export "figure3a.png", replace
	
	#delimit;

	gr tw
		(sc inft year if ideology_1 == 1 & pid3 == "Republican", col(gray) msym(Oh))
		(fpfit inft year if ideology_1 == 1 & pid3 == "Republican", col(gray) lpat(dash))
		(sc inft year if ideology_2 == 1 & pid3 == "Republican", col(gray) msym(sh))
		(fpfit inft year if ideology_2 == 1 & pid3 == "Republican", col(gray) lpat(dash))	
		,
			legend(
				order(1 "Ideologues" 3 "Moderates" 4 "Polyonmial")
				rows(1)
			)
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Year")		
			title("Republicans")
			ytitle("Average own-party rating")
			name(b, replace)
			xlab(1972(8)2016)
			;

	#delimit cr	
	
restore

*** panel (c) coefs by year ***

su ideology
replace ideology = (ideology - r(min)) / (r(max) - r(min))

levelsof year if !mi(ideology), local(levels)
foreach l of local levels {
 
	reg inft ideology `X' if  year == `l' & pid3 == "Republican" [aw=weight]
	replace Br = _b[ideology] if year == `l'

	reg inft ideology `X' if  year == `l' & pid3 == "Democrat" [aw=weight]
	replace Bd = _b[ideology] if year == `l'

}


collapse Br Bd, by(year pid3)

keep if !mi(Br)

#delimit;

gr tw 
	(sc Bd year if pid3 == "Democrat")
	(fpfit Bd year if pid3 == "Democrat", lpat(dash))
	(sc Br year if pid3 == "Republican", msym(Oh) mfcol(white))
	(fpfit Br year if pid3 == "Republican", lpat(solid))
	,
		plotregion(style(none))
		title("Coefficients")
		ytitle("Impact of moderation on rating")
		xtitle("Year")
		ylab(, angle(horiz))
		legend(
			order(
				1 "Democrats" 2 "Polynomialtrend, Democrats"
				3 "Republicans" 4 "Polynomialtrend, Republicans"
			)
		)
		xlab(1972(8)2016)
		xscale(range(1972 2016))
		name(c, replace)
		;

#delimit cr

gr combine a b c, col(3) xsize(3.5) ysize(1) scale(1.5)  

gr export "figure3.eps", replace
